package com.itheima.service.impl;

import com.itheima.dao.AnswerDao;
import com.itheima.domain.Answer;
import com.itheima.domain.User;
import com.itheima.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerDao answerDao;

    @Override
    public void save(Answer answer, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println("user = " + user);
        if (user  == null){
            answer.setUid(0);
        }else {
            answer.setUid(user.getUid());
        }

        answer.setIs_accept(0);
        answer.setLikes(0);
        answer.setCreatetime(new Date());
        System.out.println("answer2 = " + answer);
        answerDao.save(answer);
    }

    @Override
    public Boolean update(Answer answer) {
        return null;
    }

    @Override
    public Boolean delete(Integer aid) {
        int f = answerDao.delete(aid);
        return f>0;
    }

    @Override
    public Answer getById(Integer aid) {
        return null;
    }

    @Override
    public List<Answer> getAll() {
        List<Answer> answers = answerDao.getAll();
        return answers;
    }

    @Override
    public List<Answer> getByQid(Integer qid) {
        List<Answer> answerList = answerDao.getByQid(qid);
        return answerList;
    }

    @Override
    public int addlike(Integer aid) {

        return answerDao.addlike(aid);
    }
    public int cancellike(Integer aid) {

        return answerDao.cancellike(aid);
    }

    @Override
    public int answerCount(Integer qid) {

        return answerDao.answerCount(qid);
    }
}
